import { save, pages, LoadFrom, Delete, ResetPassword } from '@/api/system/userInfo'

import { pages as companyPages } from '@/api/companyInfo/companyInfo'
// import { DepartmentTree } from '@/api/systems/systemDepartment'

const state = {
  controllerName: 'userinfo',
  dataList: {
    listLoading: false,
    list: [],
    departmentTree: [],
    defaultProps: {
      children: 'departmentList',
      key: 'id',
      label: 'name'
    },
    searchForm: {
      LoginName: '',
      isAdmin: false,
      UserIdentity: 1,
      pageSize: 10,
      pageIndex: 1,
      dataCount: 0,
      pageCount: 0
    }
  },
  form: {
    formState: false,
    formDepartmentState: false,
    model: {},
    ResetPwdFormState: false,
    ResetPwdModel: {},
    companyList: []
  }
}
const mutations = {
  pageSizeChange(state, par) {
    state.dataList.searchForm.pageSize = par
    this.dispatch(`userinfo/${state.controllerName}/findList`)
  },
  currentPageChange(state, par) {
    state.dataList.searchForm.pageIndex = par
    this.dispatch(`userinfo/${state.controllerName}/findList`)
  }
}

const actions = {
  loadNewForm(context) {
    var _state = context.state
    _state.form.model = {}
    _state.form.formState = true
  },
  // 加载表单
  loadForm(context, par) {
    var _state = context.state
    console.log('加载表单参数', par)
    LoadFrom({ ID: par })
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.model = Data
        _state.form.formState = true
      })
      .catch(error => {
        console.log(error)
      })
  },
  save(context, par) {
    var _state = context.state
    par.sequenceNumber = parseInt(par.sequenceNumber)
    save(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        global.tools.msg('保存成功!', '成功')
        _state.form.formState = false

        context.dispatch('findList')
      })
      .catch(error => {
        console.log(error)
      })
  },
  findList(context, par) {
    var _state = context.state
    _state.dataList.listLoading = true
    pages(_state.dataList.searchForm)
      .then(res => {
        const { Data } = res
        console.log(Data)
        _state.dataList.list = Data.List
        _state.dataList.searchForm.pageIndex = Data.PageIndex
        _state.dataList.searchForm.pageSize = Data.PageSize
        _state.dataList.searchForm.dataCount = Data.Count
        _state.dataList.searchForm.pageCount = Data.Pages
        _state.dataList.listLoading = false
      })
      .catch(error => {
        console.log(error)
      })

    // 获取公司列表

    console.log('32222222222222222222')
    context.dispatch('getCompanyList')
  },
  delete(context, par) {
    console.log(par)
    global.tools.confirm(
      '确定要删除吗？',
      function() {
        Delete({
          id: par
        })
          .then(res => {
            const { Data } = res
            console.log(Data)
            global.tools.msg('删除成功!', '成功')
            context.dispatch('findList')
          })
          .catch(e => {
            console.log(e)
          })
      },
      null,
      '提示'
    )
  },
  restPwdFrom(context, par) {
    var _state = context.state
    _state.form.ResetPwdFormState = true
    _state.form.ResetPwdModel = {
      Id: par,
      Password: '',
      NewPassword: ''
    }
  },
  resetPassword(context, par) {
    var _state = context.state
    if (_state.form.ResetPwdModel.Password !== _state.form.ResetPwdModel.NewPassword) {
      global.tools.msg('两次密码不一致!', '提示')
      return false
    }
    ResetPassword(_state.form.ResetPwdModel)
      .then(response => {
        const { Data } = response
        console.log(Data)
        global.tools.msg('保存成功!', '成功')
        _state.form.ResetPwdFormState = false
      })
      .catch(error => {
        console.log(error)
      })
  },
  // 获取公司列表
  getCompanyList(context, val) {
    console.log(val)
    var _state = context.state
    companyPages({
      pageSize: 10,
      pageIndex: 1,
      companyName: val
    })
      .then(response => {
        const { Data } = response
        _state.form.companyList = Data.List
      })
      .catch(error => {
        console.log(error)
      })
  },
  // checkedDepartment(context) {
  //   var _state = context.state
  //   context.dispatch('loadTree')
  //   _state.form.formDepartmentState = true
  // },
  // loadTree(context) {
  //   var _state = context.state
  //   // DepartmentTree()
  //     .then(res => {
  //       const { Data } = res
  //       console.log(Data)
  //       _state.dataList.departmentTree = Data
  //       console.log(_state.dataList.departmentTree)
  //     })
  //     .catch(error => {
  //       console.log(error)
  //     })
  // },
  handleNodeClick(context, node) {
    var _state = context.state
    _state.form.model.departmentId = node.id
    _state.form.model.departmentName = node.name
    _state.form.formDepartmentState = false
  },
  handleSuccess(context, res) {
    console.log(res)
    if (res.Status === 1) {
      var _state = context.state
      _state.form.model.HeaderImage = res.Data.Url
      console.log('_state.form.model.HeaderImage', _state.form.model.HeaderImage)
    } else {
      global.tools.msg('上传失败，请重新上传！', '提示')
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
